package co.yixiang.yshop.module.product.controller.admin.campaigninfo;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.*;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductReqVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductRespVO;
import co.yixiang.yshop.module.product.convert.campaigninfo.CampaignInfoConvert;
import co.yixiang.yshop.module.product.dal.dataobject.campaigninfo.CampaignInfoDO;
import co.yixiang.yshop.module.product.service.campaigninfo.AppCampaignInfoService;
import co.yixiang.yshop.module.product.service.campaigninfo.CampaignInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 活动信息")
@RestController
@RequestMapping("/product/campaign-info")
@Validated
public class CampaignInfoController {

    @Resource
    private CampaignInfoService campaignInfoService;

    @Resource
    private AppCampaignInfoService appCampaignInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建活动信息")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:create')")
    public CommonResult<Long> createCampaignInfo(@Valid @RequestBody CampaignInfoCreateReqVO createReqVO) {
        return success(campaignInfoService.createCampaignInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动信息")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:update')")
    public CommonResult<Boolean> updateCampaignInfo(@Valid @RequestBody CampaignInfoUpdateReqVO updateReqVO) {
        campaignInfoService.updateCampaignInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:campaign-info:delete')")
    public CommonResult<Boolean> deleteCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.deleteCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/close")
    @Operation(summary = "结束活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:campaign-info:close')")
    public CommonResult<Boolean> closeCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.closeCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/start")
    @Operation(summary = "开始活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:campaign-info:start')")
    public CommonResult<Boolean> startCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.startCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:get')")
    public CommonResult<CampaignInfoRespVO> getCampaignInfo(@RequestParam("id") Long id) {
        return success(campaignInfoService.getCampaignInfo(id));
    }

    @GetMapping("/getByType")
    @Operation(summary = "获得活动信息")
    @Parameter(name = "type", description = "活动类型", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:get')")
    public CommonResult<CampaignInfoRespVO> getCampaignInfoByType(@RequestParam("type") Integer type) {
        return success(campaignInfoService.getCampaignInfoByType(type));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动信息分页")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:query')")
    public CommonResult<PageResult<CampaignInfoPageRespVO>> getCampaignInfoPage(@Valid CampaignInfoPageReqVO pageVO) {
        return success(campaignInfoService.getCampaignInfoPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:export')")
    @OperateLog(type = EXPORT)
    public void exportCampaignInfoExcel(@Valid CampaignInfoExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<CampaignInfoDO> list = campaignInfoService.getCampaignInfoList(exportReqVO);
        // 导出 Excel
        List<CampaignInfoExcelVO> datas = CampaignInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "活动信息.xls", "数据", CampaignInfoExcelVO.class, datas);
    }

    @GetMapping("/data")
    @Operation(summary = "获得活动数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:data')")
    public CommonResult<CampaignDataRespVO> getCampaignData(@RequestParam("id") Long id) {
        return success(campaignInfoService.getCampaignData(id));
    }

    @GetMapping("/data-detail")
    @Operation(summary = "获得活动信息分页")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:data-detail')")
    public CommonResult<PageResult<CampaignDataDetailRespVO>> getCampaignDataDetail(@Valid CampaignDataDetailPageReqVO pageVO) {
        return success(campaignInfoService.getCampaignDataDetail(pageVO));
    }

    @GetMapping("/canvas/page")
    @Operation(summary = "获得画布活动分页")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:canvas:page')")
    public CommonResult<PageResult<CanvasCampaignPageRespVO>> getCanvasCampaignPage(@Valid CanvasCampaignPageReqVO pageVO) {
        return success(campaignInfoService.getCanvasCampaignPage(pageVO));
    }

    @PostMapping("/canvas/product-page")
    @Operation(summary = "获得营销活动商品列表")
    @PreAuthorize("@ss.hasPermission('product:campaign-info:canvas:product-page')")
    public CommonResult<PageResult<CampaignProductRespVO>> getCampaignProductPage(@RequestBody CampaignProductReqVO reqVO) {
        return success(appCampaignInfoService.getCampaignProductPage(reqVO));
    }


}
